package cn.meetingroom.controller;

import cn.meetingroom.model.Room;
import cn.meetingroom.model.User;
import cn.meetingroom.service.RoomService;
import org.codehaus.jackson.map.ObjectMapper;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/RoomServlet/*")
public class RoomServlet extends BaseServlet {
    RoomService roomService = new RoomService();
    ObjectMapper mapper =  new ObjectMapper();
    public void addRoom(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        Room room = new Room();

        String imgUrl = request.getSession().getAttribute("rImg").toString();
        if (imgUrl != null && !  imgUrl.equals(""))
        room.setImgUrl(imgUrl);
        room.setNum(request.getParameter("roomNum"));
        room.setRoomName(request.getParameter("roomName"));
        room.setPlace(request.getParameter("roomPlace"));
        room.setCapacity(Integer.parseInt(request.getParameter("roomCapacity")));
        room.setIntroduction(request.getParameter("roomIntroduction"));

        if (roomService.addRoom(room)){
            response.getWriter().print("添加成功!!!");
        } else {
            response.getWriter().print("添加失败!!!");
        }
    }

    public void RoomList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");

        mapper.writeValue(response.getWriter(),roomService.getRoomList());
    }

    public void getRoomState(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");

        mapper.writeValue(response.getWriter(),roomService.getRoomState());
    }

    public void RoomApplyList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");

        mapper.writeValue(response.getWriter(),roomService.getUntreatedRoom());
    }

    public void getRoomStateByUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");

        User user = (User) request.getSession().getAttribute("user");

        mapper.writeValue(response.getWriter(),roomService.getRoomStateByUser(user.getId()));
    }

    public void getRoomById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        int id;
        if (request.getSession().getAttribute("rid") == null){
            id = Integer.parseInt(request.getParameter("rid"));
        }else {
            id = Integer.parseInt(request.getSession().getAttribute("rid").toString());
            request.getSession().removeAttribute("rid");
        }

        mapper.writeValue(response.getWriter(),roomService.getRoomById(id));
    }

    public void fuzzySearch(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        String value = request.getParameter("value");

        mapper.writeValue(response.getWriter(),roomService.fuzzS(value));
    }

    public void saveRid(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.getSession().setAttribute("rid",request.getParameter("rid"));
    }
}
